package dominion.model.actions.base;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import dominion.model.Card;
import dominion.model.GameBoard;
import dominion.model.Player;
import dominion.model.actions.Action;
import dominion.utils.Constants;

public class Chapel extends Action {

	public Chapel() {
		this.cost = 2;
	}

	@Override
	public Card clone() {
		return new Chapel();
	}

	@Override
	public String toString() {
		return "Chapel";
	}

	@Override
	public String getDescription() {
		StringBuffer sb = new StringBuffer();
		sb.append(super.getDescription());
		sb.append("Trash up to 4 cards from your hand\n");
		return sb.toString();
	}

	@Override
	public void doAction(Player player, GameBoard board, List<Player> players) throws IOException {
		player.changeAction(-1);
		playedCard(player);

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String choice = null;
		int discardsLeft = 4;
		Constants.printer.print("Type a card name to discard or \"done\" to finish.");
		Constants.printer.print(player.printHand());
		Constants.printer.print("You may discard up to " + discardsLeft + " more.");
		while (!player.getHand().isEmpty() && discardsLeft > 0)
		{
			choice = in.readLine().trim().toLowerCase();
			if (choice.equalsIgnoreCase("done"))
			{
				discardsLeft = 0;
				continue;
			}
			if (!player.getHand().contains(choice))
			{
				Constants.printer.print("Card is not in hand.");
			}
			else
			{
				Card card = player.getHand().drawCard(choice);
				board.addToTrash(card);
				discardsLeft--;
			}
		}
		Constants.printer.print("New hand: ");
		Constants.printer.print(player.printHand());
	}
}
